package dbAccess;

import java.io.*;
import java.util.*;
import java.util.Date;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Finance {

	private final static String defaultStr = "unknown";
	private int FinanceId;
	private String EmailAddress;	
	private int SIN;
	private String FirstName;
	private String LastName;
	private String PhoneNum;
	private String HomeAddress;
	private String gender;
	private java.sql.Date dateOfBirth;
	private String password;
	private String creator;
	
	private String statusMsg;

	public Finance(String creatorIn)
	{
		FinanceId=-1;
		SIN=-1;
		FirstName = defaultStr;
		LastName = defaultStr;
		EmailAddress = defaultStr;
		PhoneNum = defaultStr;
		HomeAddress = defaultStr;
		gender = defaultStr;
		dateOfBirth= java.sql.Date.valueOf("0000-00-00");
		password = defaultStr;
		statusMsg = "";
		creator = creatorIn;
	}
	
	
	public Boolean registerFinance(String action)
	{
		if(action.equals("update"))
		{
			int financeId = this.getFinanceId();
			String updateQuery = "update tbl_finance set IsDeleted = '1' where FinanceId = " + financeId ;
			DbAccess access = new DbAccess();
			access.ExecuteUpdate(access.connect(), updateQuery);
			Login.Log(creator, "update tbl_finance set IsDeleted = '1' where FinanceId = " + financeId);
			
		}
		this.setStatusMsg("");
		String query =  "select SIN, EmailAddress from tbl_finance where (SIN = " + SIN + 
						" or EmailAddress = '" + EmailAddress + "') AND IsDeleted='0'";
		
		DbAccess access = new DbAccess();
		
		try 
		{
			ResultSet rs = access.ExecuteSelect(access.connect(), query);
			if (rs.next())
			{
				this.setStatusMsg("Financial Officer already exists.");
				return false;
			}
		}
		catch (SQLException e)
		{
			this.setStatusMsg("Duplication check error occurred: " + e.getMessage());
			return false;
		}
		
		//insert into tbl_person
		query =  "insert into tbl_finance ";
		query += "(EmailAddress, SIN, FirstName, LastName, Phone, HomeAddress, gender, " +
				 "DateOfBirth, Password, IsDeleted)";
		query += "values ('" + EmailAddress + "', '" + SIN + "', '"+ FirstName + "', '" + 
				 LastName + "', '" + PhoneNum + "', '" + HomeAddress + "', '"+ gender + "', '" + 
				 dateOfBirth + "', '" + password + "', '0')"; 
		
		if (!(access.ExecuteUpdate(access.connect(), query)))
		{
			this.setStatusMsg("Could not update tbl_finance with financial officer information.");
			return false;
		}
		else
		{
			Login.Log(creator,query);
		}
		
		
		//store doc ID field
		if (!(evalFinanceId()))
		{
			return false;
		}
		
		if(action.equals("update"))
		{
			this.setStatusMsg(this.getFirstName() + " " + this.getLastName() + " updated with new ID: " + this.getFinanceId());
			return true;	
		}
		
		this.setStatusMsg("New financial officer created with ID: " + this.getFinanceId());
		return true;
	}
	
	
	private Boolean evalFinanceId()
	{
		DbAccess access = new DbAccess();
		String query =  "select tbl_finance.FinanceId from tbl_finance where tbl_finance.EmailAddress = '" + 
						this.EmailAddress + "' and IsDeleted = 0";
		
		try
		{
			ResultSet rs = access.ExecuteSelect(access.connect(), query);
			rs.next();
			this.FinanceId = rs.getInt("tbl_finance.FinanceId");
			return true;
		}
		catch (SQLException e)
		{
			return false;
		}
	}
	
		//for admins
	public Boolean removeFinance(int FinanceId)
	{
		DbAccess access = new DbAccess();
		
		
		//update finance table
		String query = "UPDATE tbl_finance SET IsDeleted=1 WHERE FinanceId='" + FinanceId + "'";
		if(!access.ExecuteUpdate(access.connect(), query))
		{
			this.setStatusMsg("failed to update financial officer records");
			return false;
		}
		else
		{
			Login.Log(creator, query);
		}
		
		Date now = new Date();
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
		
		this.setStatusMsg("Financial Officer has been deleted");
		return true;
	}
	
	public int getFinanceId() {
		return FinanceId;
	}

	public void setFinanceId(int FinanceId) {
		this.FinanceId = FinanceId;
	}

	public int getSIN() {
		return SIN;
	}

	public void setSIN(int sin) {
		SIN = sin;
	}

	public String getFirstName() {
		return FirstName;
	}

	public void setFirstName(String firstName) {
		FirstName = firstName;
	}

	public String getLastName() {
		return LastName;
	}

	public void setLastName(String lastName) {
		LastName = lastName;
	}

	public String getPhoneNum() {
		return PhoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		PhoneNum = phoneNum;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getStatusMsg() {
		return statusMsg;
	}

	public void setStatusMsg(String statusMsg) {
		this.statusMsg = statusMsg;
	}
	public String getFinanceEmailAddress() {
		return EmailAddress;
	}

	public void setFinanceEmailAddress(String FinanceEmailAddress) {
		EmailAddress = FinanceEmailAddress;
	}
	public String getHomeAddress() {
		return HomeAddress;
	}


	public void setHomeAddress(String homeAddress) {
		HomeAddress = homeAddress;
	}


	public String getGender() {
		return gender;
	}


	public void setGender(String gender) {
		this.gender = gender;
	}

	public java.sql.Date getDateOfBirth() {
		return dateOfBirth;
	}


	public void setDateOfBirth(java.sql.Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	
	
	
}
